﻿using BBCore.Core.Models.Admins;
using BBCore.Infrastructure.Helps;
using BootstrapBlazor.Components;
using ClosedXML.Excel;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using System.Data;
using System.Diagnostics.CodeAnalysis;


namespace BBCore.Server.Components.Pages.Samples
{
    /// <summary>
    /// 单个sql查询,没有参数的查询
    /// </summary>
    public partial class SingleSqlToExcel
    {
        [Parameter]
        public int AuthId { get; set; }
        [Inject]
        [NotNull]
        private IJSRuntime? JS { set; get; }
        [Inject]
        [NotNull]
        private NavigationManager? NavigationManager { get; set; }
        public MenuEntity Webpages = new();
        private DataTable Dt { get; set; } = new();
        private readonly ExportDataFile _export = new();
        private readonly XLWorkbook _workbook = new();

        protected override void OnInitialized()
        {
            Webpages = IAdminService.menu_Getmenu(AuthId);
        }
        private async Task QueryToExcelAsync()
        {
            Dt = await ITempService.GetFqlToDtAsync(Webpages.Query_Sql!);
            try
            {
                _workbook.Worksheets.Add(Dt, "RESULT");
                await ExportToExcel();
            }
            catch (Exception ex)
            {
                //nlogILogger?.LogError("单一SQL导出Excel出错:\r\n" + ex.ToString() + "\r\n");
                //throw new Exception(ex.ToString());
                await SwalService.Show(new SwalOption()
                {
                    Category = SwalCategory.Error,
                    Title = "程序出错了!",
                    Content = "出错信息:" + ex.Message
                });
            }
        }
        private async Task QueryToCsvAsync()
        {
            Dt = await ITempService.GetFqlToDtAsync(Webpages.Query_Sql!);
            try
            {
                _workbook.Worksheets.Add(Dt, "CSV");
                await ExportToCsv();
            }
            catch (Exception ex)
            {
                nlogILogger?.LogError("单一SQL导出Excel出错:\r\n" + ex.ToString() + "\r\n");

                //throw new Exception(ex.ToString());
            }
        }
        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <returns></returns>
        private async Task ExportToExcel()
        {
            try
            {
                await _export.ExportToFileAsync(JS, _workbook, Webpages.MenuName!, MimeType.Excel);
            }
            catch (Exception ex)
            {
                nlogILogger?.LogError("单一SQL导出Excel出错:\r\n" + ex.ToString() + "\r\n");

                //throw new Exception(ex.ToString());
            }
            //NavigationManager.NavigateTo(NavigationManager.Uri, forceLoad: true);
        }
        /// <summary>
        /// 导出CSV
        /// </summary>
        /// <returns></returns>
        private async Task ExportToCsv()
        {
            try
            {
                await _export.ExportToFileAsync(JS, _workbook, Webpages.MenuName!, MimeType.Csv);
            }
            //NavigationManager.NavigateTo(NavigationManager.Uri, forceLoad: true);
            catch (Exception ex)
            {
                nlogILogger?.LogError("单一SQL导出Excel出错:\r\n" + ex.ToString() + "\r\n");
                //throw new Exception(ex.ToString());
            }
        }
    }
}
